import { request } from '@/utils/request'

interface ResponseData<T> {
  code: number
  data?: T
  msg: string
}

// 获取经书分页的参数接口
export interface GetSutrasPageParams {
  /** 经书名称 */
  sutraName?: string
  /** 页码，从 1 开始 */
  pageNo: number
  /** 每页条数，最大值为 100 */
  pageSize: number
}

export interface MdlSutrasRespVO {
  /** id */
  id: number
  /** 封面 */
  coverUrl: string
  /** 经书名称 */
  sutraName: string
  /** 经书简介 */
  description?: string
  /** 状态 */
  status?: number
  /** 创建时间 */
  createTime?: string
}

export interface PageResultMdlSutrasRespVO {
  /** 数据 */
  list: MdlSutrasRespVO[]
  /** 总量 */
  total: number
}

/** 获得经书主分页 GET /app-api/mdl/sutras/page */
export const getSutrasPage = async (
  params: GetSutrasPageParams,
): Promise<ResponseData<PageResultMdlSutrasRespVO>> => {
  try {
    const response = await request.get<PageResultMdlSutrasRespVO>('/app-api/mdl/sutras/page', {
      params,
    })
    return response
  } catch (error) {
    console.error('获取经书分页失败:', error)
    return {
      code: -1,
      msg: '网络请求失败',
      data: {
        list: [],
        total: 0,
      },
    }
  }
}

// 获取经书章节列表的参数接口
export interface GetSutrasChapterListParams {
  /** 经书ID */
  sutraId: string | number
}

// 经书章节响应接口
export interface AppMdlSutrasChapterRespVO {
  /** 章节ID */
  id: string | number
  /** 章节标题 */
  chapterTitle: string
}

// 经书章节分页结果接口
export interface PageResultAppMdlSutrasChapterRespVO {
  /** 数据 */
  list: AppMdlSutrasChapterRespVO[]
  /** 总量 */
  total: number
}

/** 获取章节列表 GET /app-api/mdl/sutras/chapter/list */
export const getSutrasChapterList = async (
  params: GetSutrasChapterListParams,
): Promise<ResponseData<PageResultAppMdlSutrasChapterRespVO>> => {
  try {
    const response = await request.get<PageResultAppMdlSutrasChapterRespVO>(
      '/app-api/mdl/sutras/chapter/list',
      {
        params,
      },
    )
    return response
  } catch (error) {
    console.error('获取经书章节列表失败:', error)
    return {
      code: -1,
      msg: '网络请求失败',
      data: {
        list: [],
        total: 0,
      },
    }
  }
}

/** 获取章节内容的参数接口 */
export interface GetSutrasChapterContentParams {
  /** 经书ID */
  sutraId: string | number
  /** 章节ID */
  chapterId: string | number
}

/** 章节内容响应接口 */
export interface AppMdlSutrasChapterContentRespVO {
  /** 章节ID */
  id: string | number
  /** 章节标题 */
  chapterTitle: string
  /** 章节副标题 */
  chapterSubtitle: string
  /** 章节内容 */
  content: string
}

/** 获取章节内容 GET /app-api/mdl/sutras/chapter/get */
export const getSutrasChapterContent = async (
  params: GetSutrasChapterContentParams,
): Promise<ResponseData<AppMdlSutrasChapterContentRespVO>> => {
  try {
    const response = await request.get<AppMdlSutrasChapterContentRespVO>(
      '/app-api/mdl/sutras/chapter/get',
      {
        params,
      },
    )
    return response
  } catch (error) {
    console.error('获取章节内容失败:', error)
    return {
      code: -1,
      msg: '网络请求失败',
    }
  }
}
